package com.dubito.palette

import android.graphics.*
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.palette.graphics.Palette
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition


class ImageActivity : AppCompatActivity() {

    private lateinit var imageView: ImageView
    private lateinit var ivBg: ImageView
    private lateinit var btn: Button
    private val images = intArrayOf(R.drawable.fx, R.drawable.fj, R.drawable.jt, R.drawable.pg)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_image)
        imageView = findViewById(R.id.imageView)
        ivBg = findViewById(R.id.iv_bg)
        btn = findViewById(R.id.change)
        setImage(R.drawable.fj)
        btn.setOnClickListener {
            setImage(images[(0..3).random()])
        }

    }

    private fun setImage(img: Int) {
        Glide.with(this)
            .asBitmap().load(img)
            .into(object : CustomTarget<Bitmap>(300, 300) {
                override fun onLoadCleared(placeholder: Drawable?) {
                }

                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    Palette.from(resource).generate {
                        if (it == null) return@generate
                        when (Color.TRANSPARENT) {
                            it.getDarkVibrantColor(Color.TRANSPARENT) -> {
                                createLinearGradientBitmap(
                                    it.getDarkVibrantColor(Color.TRANSPARENT),
                                    it.getVibrantColor(Color.TRANSPARENT)
                                )
                            }
                            it.getDarkMutedColor(Color.TRANSPARENT) -> {
                                createLinearGradientBitmap(
                                    it.getDarkMutedColor(Color.TRANSPARENT),
                                    it.getMutedColor(Color.TRANSPARENT)
                                )
                            }
                            else -> {
                                createLinearGradientBitmap(
                                    it.getLightMutedColor(Color.TRANSPARENT),
                                    it.getLightVibrantColor(Color.TRANSPARENT)
                                )
                            }
                        }
                    }
                    imageView.setImageBitmap(handleBitmap(resource))
                }
            })
    }

    //创建线性渐变背景色
    private fun createLinearGradientBitmap(darkColor: Int, color: Int) {
        val bgColors = IntArray(2)
        bgColors[0] = darkColor
        bgColors[1] = color
        val bgBitmap =
            Bitmap.createBitmap(ivBg.width, ivBg.height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas()
        val paint = Paint()
        canvas.setBitmap(bgBitmap)
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)
        val gradient = LinearGradient(
            0f, 0f, 0f, (bgBitmap.height).toFloat(),
            bgColors[0],
            bgColors[1],
            Shader.TileMode.CLAMP
        )
        paint.shader = gradient
        paint.isAntiAlias = true
        val rectF = RectF(0f, 0f, bgBitmap.width.toFloat(), bgBitmap.height.toFloat())
        canvas.drawRoundRect(rectF, 20f, 20f, paint)
        canvas.drawRect(rectF, paint)
        ivBg.setImageBitmap(bgBitmap)
    }

    //修改透明度
    private fun getImageToChange(mBitmap: Bitmap): Bitmap? {
        val createBitmap =
            Bitmap.createBitmap(mBitmap.width, mBitmap.height, Bitmap.Config.ARGB_8888)
        val mWidth = mBitmap.width
        val mHeight = mBitmap.height
        for (i in 0 until mHeight) {
            for (j in 0 until mWidth) {
                var color = mBitmap.getPixel(j, i)
                val g = Color.green(color)
                val r = Color.red(color)
                val b = Color.blue(color)
                var a = Color.alpha(color)
                //从中间部分开始透明渐变
                val index = i * 1.0f / mHeight
                if (index > 0.5f) {
                    a = 255 - (i / (mHeight / 2f) * 255).toInt()
                }
                color = Color.argb(a, r, g, b)
                createBitmap.setPixel(j, i, color)
            }
        }
        return createBitmap
    }

    /**
     * 通过位移运算来做透渐变，相比之前的方法提高90倍左右
     * @param bitmap
     * @return
     */
    private fun handleBitmap(bitmap: Bitmap): Bitmap? {
        //透明渐变
        val argb = IntArray(bitmap.width * bitmap.height)
        bitmap.getPixels(argb, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height)

        //循环开始的下标，设置从什么时候开始改变
        val start = argb.size / 2

        val width = bitmap.width
        for (i in 0 until bitmap.height / 2 + 1) {
            for (j in 0 until width) {
                val index = start - width + i * width + j
                if (argb[index] != 0) {
                    argb[index] =
                        ((1 - i / (bitmap.height / 2f)) * 255).toInt() shl 24 or (argb[index] and 0x00FFFFFF)
                }
            }
        }
        return Bitmap.createBitmap(argb, bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)
    }

}